package com.example.scaffold_demo.ui;

import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.OnClick;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.scaffold_demo.R;
import com.example.scaffold_demo.base.BaseActivity;
import com.example.scaffold_demo.db.DBManager;
import com.example.scaffold_demo.db.entity.CreditCard;
import com.example.scaffold_demo.db.entity.People;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Author: 李巷阳
 * Date: 2020/9/2
 * Description: 数据库activity
 */
public class DBActivity extends BaseActivity {


    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.tv_add)
    Button tvAdd;
    @BindView(R.id.tv_delete)
    Button tvDelete;
    @BindView(R.id.tv_update)
    Button tvUpdate;
    @BindView(R.id.tv_select)
    Button tvSelect;
    @BindView(R.id.tv_select_one)
    Button tvSelectOne;
    @Override
    protected void onResume() {
        super.onResume();
        //查找,并显示到页面
        selectAll();
    }
    @Override
    protected void initTitle() {

    }

    @Override
    protected int setLayout() {
        return R.layout.activity_d_b;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
    }

    @Override
    public void showError(String msg) {

    }

    @OnClick({R.id.tv_add, R.id.tv_delete, R.id.tv_update, R.id.tv_select, R.id.tv_select_one})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            // 增加
            case R.id.tv_add:
                Random mRandom = new Random();
                Long peopleId = Long.valueOf(mRandom.nextInt(10000) + 10);
                People people = new People();
                people.setId(peopleId);
                people.setName("李阳");

                ArrayList ccList = new ArrayList();
                //插入对应的CreditCard数据,一个人拥有多个信用卡
                for (int j = 0; j < 3; j++) {
                    Long creditCardId = Long.valueOf(mRandom.nextInt(10000) + 10);
                    CreditCard creditCard = new CreditCard();
                    creditCard.setId(creditCardId);
                    // 设置信用卡关联至 人的id
                    creditCard.setPeopleId(people.getId());
                    creditCard.setCardNum("李阳的银行卡号");
                    // 添加至数据库
                    ccList.add(creditCard);
                }
                // 添加直接往数据库 一个集合 里面会自动遍历
                DBManager.getInstance(getApplicationContext()).insert(ccList);
                DBManager.getInstance(getApplicationContext()).insert(people);
                selectAll();

                break;
            // 删除
            case R.id.tv_delete:
                DBManager.getInstance(getApplicationContext()).deleteAll(People.class);
                //查找,并显示到页面
                selectAll();
                break;
            // 修改
            case R.id.tv_update:
                List<People> mPeopleList = selectAll();
                if (mPeopleList != null && mPeopleList.size() > 0) {
                    People mPeople = mPeopleList.get(0);
                    mPeople.setName("婷婷");
                    List creditCardsList = mPeople.getCreditCardsList();
                    if (creditCardsList != null && creditCardsList.size() > 0) {
                        CreditCard creditCard = (CreditCard) creditCardsList.get(0);
                        creditCard.setCardNum("婷婷的银行卡号");
                    }
                    DBManager.getInstance(getApplicationContext()).update(creditCardsList);
                    DBManager.getInstance(getApplicationContext()).update(mPeople);
                    selectAll();
                }
                break;
            // 查看
            case R.id.tv_select:
                //查找,并显示到页面
                selectAll();
                break;
            // 查看第一条
            case R.id.tv_select_one:
                //查找
                List<People> peopleList = DBManager.getInstance(getApplicationContext()).quaryAll(People.class);
                if(peopleList!=null&&peopleList.size()>0)
                {
                    //查找单个
                    People p = DBManager.getInstance(getApplicationContext()).quaryByKey(People.class, peopleList.get(0).getId()+"");
                    tvContent.setText(p.toString());
                }
                break;
        }
    }


    /**
     * 查找全部
     */
    private List<People> selectAll() {

        List<People> peopleList = DBManager.getInstance(getApplicationContext()).quaryAll(People.class);
        if (peopleList != null && peopleList.size() > 0) {
            //查找单个
            tvContent.setText(peopleList.toString());
        }else{
            //查找单个
            tvContent.setText("");
        }
        return peopleList;
    }
}